﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataSync.Entity;
using System.Data;
using Oracle.ManagedDataAccess.Client;
using DataSync.Common;

namespace DataSync.Proc.Writer
{
    class OracleDBWriter : DBWriter
    {
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected override void DBInsert(IList<Dictionary<string, ColData>> data, IDbConnection conn)
        {
            //base.DBInsert(data, conn);

            var myconn = conn as OracleConnection;
            string sql = CurOutputDbConf.Sqls[CNT_Sql_Ins]; //入库sql
            using (var trs = myconn.BeginTransaction())  //开启事务
            {
                var cmd = myconn.CreateCommand();
                cmd.CommandText = sql;
                cmd.ArrayBindCount = data.Count;
                cmd.BindByName = true;  //oracle必须指定 默认为按照顺序绑定

                foreach (var col in data[0])
                {
                    var p = cmd.CreateParameter();
                    p.ParameterName = col.Key;
                    p.DbType = DBFactory.GetDbType(col.Value.FieldType);

                    p.Value = data.Select(c => DataTypeHelper.ConvertToType(col.Value.FieldType, c[col.Key].Value)).ToArray();
                    p.Direction = ParameterDirection.Input;
                    cmd.Parameters.Add(p);
                }
                cmd.ExecuteNonQuery();

                trs.Commit();
            }
        }

    }
}
